<?php

namespace app\admin\model;
use Qiniu\Auth;
use Qiniu\Config;
use Qiniu\Storage\BucketManager;
use Qiniu\Storage\UploadManager;
class Qiniu extends Model{
    private $accessKey='';
    private $domain='';
    private $secretKey='';
    private $bucket='';
    public function upload($files)
    {
        // 初始化签权对象
        $auth = new Auth($this->accessKey,$this->secretKey);
        $token = $auth->uploadToken($this->bucket);
        // 构建 UploadManager 对象
        $uploadMrg = new UploadManager();
        // 上传文件到七牛

        $saveName = hash_file('sha1', $files['file']['tmp_name']) . time();
        list($ret, $err) = $uploadMrg->putFile($token, $saveName, $files['file']['tmp_name']);
        $a['url']= $this->domain.$ret['key'];
        $a['key']= $ret['key'];
        if ($err !== null) {
            $return =  [
                'code'  =>  0,
                'msg'   =>  '上传图片失败',
            ];
        } else {
            $return =  [
                'code'  =>  1,
                'msg'   =>  '上传成功',
                'key'   => $a,
            ];
        }
        return $return;
    }

    /**
     * @param $name//array
     * @return array
     */
    public  function  del($name){
        $auth = new Auth($this->accessKey, $this->secretKey);

        $bucketMgr = new BucketManager($auth);

        $error = $bucketMgr->delete($this->bucket, $name);
        if ($error['0'] !== null) {
            $return =  [
                'code'  =>  0,
            ];
        }else{
            $return =  [
                'code'  =>  1,
            ];
        }
        return $return;
    }
}
